package nciae.db.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import nciae.db.common.CommonResult;
import nciae.db.mapper.CourseMapper;
import nciae.db.mapper.StudentMapper;
import nciae.db.pojo.dto.ApiDeleteCourseFileDTO;
import nciae.db.pojo.dto.ApiGetTableDto;
import nciae.db.pojo.entity.Course;
import nciae.db.pojo.entity.Student;
import nciae.db.pojo.vo.ApiTimeTableVO;
import nciae.db.pojo.vo.CourseFilesVo;
import nciae.db.pojo.vo.CourseVO;
import nciae.db.service.CourseService;
import nciae.db.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import nciae.db.common.CommonResult;
import nciae.db.pojo.entity.Course;
import nciae.db.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping("/course")
@Slf4j
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseMapper courseMapper;

    @GetMapping("/list")
    public CommonResult page(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer limit,
                             @RequestParam(required = false) Integer projectId,
                             @RequestParam(required = false) String lessonDate,
                             HttpServletRequest request){
        log.info("分页查询班级参数:{} {} {} {} {}", page, limit, projectId, lessonDate);
        // 调用 Service 层的查询方法
        CommonResult searchResult = courseService.search(page, limit, projectId, lessonDate, request);
        return searchResult;
    }
    @GetMapping("/detail")
    public CommonResult<Course> queryDetailById(Integer courseId){
        CommonResult<Course> result = new CommonResult<>();
        result.setCode(0);
        result.setData(courseService.getById(courseId));
        return result;
    }
    @GetMapping("/delete")
    public CommonResult deleteById(Integer courseId){
        CommonResult result = new CommonResult<>();
        try {
            if (courseService.deleteById(courseId)) {
                result.setCode(0);
                result.setMsg("删除成功");
            }
            else
            {
                result.setCode(400);
                result.setMsg("删除失败");
            }
            return result;
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("删除异常: " + e.getMessage());
            return result;
        }
    }
    @RequestMapping("/save")
    public CommonResult save(@RequestBody Course course)
    {
        System.out.println(course.toString());
        return courseService.saveLog(course);

    }

    //    课件上传文件
    @PostMapping("/uploadFile")
    public CommonResult<String> uploadFile(@RequestParam("file") MultipartFile file,
                                   @RequestParam("courseId") Integer courseId) {
        CommonResult<String> commonResult = new CommonResult<>();

        if (courseService.updateCourseFile(file, courseId)){
            commonResult.setCode(0);
            commonResult.setMsg("文件上传成功");
            commonResult.setData("文件上传成功");
        } else {
            commonResult.setCode(-1);
            commonResult.setMsg("文件上传失败");
            commonResult.setData("文件上传失败");
        }

        return commonResult;
    }


    //获得文件信息
    @GetMapping("/getCourseFiles")
    public CommonResult<CourseFilesVo> getCourseFiles(@RequestParam Integer courseId) {
        CommonResult<CourseFilesVo> result = new CommonResult<>();
        result=courseService.getCourseFiles(courseId);
        return result;
    }


    //删除文件
    @GetMapping("/deleteFile")
    public CommonResult deleteFile(@RequestParam Integer courseId, @RequestParam String fileName) {

        CommonResult result = new CommonResult<>();
        try {

            System.out.println("老走水水水水地方都是v");
             ApiDeleteCourseFileDTO apiDeleteCourseFileDTO = new ApiDeleteCourseFileDTO();
             apiDeleteCourseFileDTO.setCourseId(courseId);
             apiDeleteCourseFileDTO.setFileName(fileName);
             apiDeleteCourseFileDTO.setUrl_image(courseMapper.selectById(courseId).getUrlImg());
//            返回删除的文件名带uuid的
             result= FileUploadUtils.deleteCourseFile(apiDeleteCourseFileDTO);
            if (result.getCode()==200) {
                result.setCode(0);
                result.setMsg("文件删除成功!!!!!");
                }
            else
            {
                result.setCode(-1);
                result.setMsg("文件删除失败");

            }
        } catch (Exception e) {
            result.setCode(-1);
            result.setMsg("文件删除异常: " + e.getMessage());
        }
        return result;
    }

}